package com.hjjt.baselib.util

import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.graphics.PointF
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.hjjt.baselib.CalculateType
import com.sunstar.baselib.R
import java.io.InputStream
import java.math.BigDecimal

/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjiot.viewlib.util
 * @ClassName:      Extension
 * @Description:    扩展函数
 * @Author:         孙浩
 * @CreateDate:     2022/6/15 9:08
 */
fun ImageView.loadImage(url: String) {
    loadImage(url, null, null, false)
}

fun ImageView.loadImage(url: String, placeholder: Drawable?) {
    val loadUrl = url ?: ""
    loadImage(loadUrl, placeholder, null, false)
}


fun ImageView.loadRoundImg(url: String, placeholder: Drawable?) {
    val loadUrl = url ?: ""
    Glide.with(this).load(loadUrl).placeholder(placeholder ?: ColorDrawable(Color.GREEN))
        .transform(CircleCrop()).into(this)
}

fun ImageView.loadImageWithId(id: Int) {
    loadImageWithId(id, false)
}

fun ImageView.loadImageWithId(id: Int, isGray: Boolean) {
    Glide.with(this).load(id).run {
        if (isGray) {
            transform(GreyPicTransform())
        } else {
            this
        }
    }.into(this)
}


fun ImageView.loadImage(url: String, placeholder: Drawable?, radius: Int?, isGray: Boolean) {
    val loadUrl = url ?: ""
    if (loadUrl.isEmpty()) return;

    Glide.with(this).load(loadUrl).placeholder(placeholder).run {
        if (null != radius && 0 != radius) {
            val displayRadius = DisplayUtils.dp2px(context, radius.toFloat())
            transform(CenterCrop(), RoundedCorners(displayRadius))
        } else {
            this
        }
    }.run {
        if (isGray) {
            transform(GreyPicTransform())
        } else {
            this
        }
    }.into(this)
}

val View.centerTop: PointF
    get() {
        val location = IntArray(2)
        this.getLocationInWindow(location)
        val x = location[0] + this.width / 2
        val y = location[1] - this.height / 2
        return PointF(x.toFloat(), y.toFloat())
    }

fun Activity.showDialog(binding: ViewDataBinding): Dialog {
    val dialog = Dialog(this, R.style.TransparentDialog).apply {
        setContentView(binding.root)
    }
    return dialog
}

fun Activity.showDialog(view: View): Dialog {
    val dialog = Dialog(this, R.style.TransparentDialog).apply {
        window?.setDimAmount(0f)
        setContentView(view)
    }
    return dialog
}


fun Double.decimalCalculate(targetNumber: Int, type: CalculateType): Double {
    val d1 = BigDecimal(this)
    val d2 = BigDecimal(targetNumber)
    val dResult = when (type) {
        CalculateType.ADD -> d1.add(d2)
        CalculateType.SUB -> d1.subtract(d2)
        CalculateType.MULTIPLICATION -> d1.multiply(d2)
        CalculateType.DIV -> d1.divide(d2)
    }
    return dResult.toDouble()
}

fun Double.decimalCalculate(targetNumber: Double, type: CalculateType): Double {
    val d1 = BigDecimal(this)
    val d2 = BigDecimal(targetNumber)
    val dResult = when (type) {
        CalculateType.ADD -> d1.add(d2)
        CalculateType.SUB -> d1.subtract(d2)
        CalculateType.MULTIPLICATION -> d1.multiply(d2)
        CalculateType.DIV -> d1.divide(d2)
    }
    return dResult.toDouble()
}

fun Any.logError(msg: String) {
    Log.e(this.javaClass.simpleName, msg)
}

fun Any.logInfo(msg: String) {
    Log.i(this.javaClass.simpleName, msg)
}

fun Any.debug(msg: String) {
    Log.d(this.javaClass.simpleName, msg)
}

fun Any.logWarring(msg: String) {
    Log.w(this.javaClass.simpleName, msg)
}


